package com.svs.system.service;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;

import javax.imageio.ImageIO;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.svs.common.config.Global;
import com.svs.framework.util.ImageUtil;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.Thumbnails.Builder;

@Service
public class UploadService {


	public String handleFileUpload(MultipartFile file, String dir) throws Exception {
		// 判断文件名
		String fileName = file.getOriginalFilename();
		String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
		String uuid = UUID.randomUUID().toString();
		String bigPath = String.format(Global.getProfile()+"/%s/%s_b.%s", dir, uuid, fileExt);
		write(Paths.get(Global.getProfile(), bigPath), file.getBytes());
		String midPath = String.format(Global.getVisitUrl()+"/%s/%s.%s", dir, uuid, fileExt);
		write(Paths.get(Global.getProfile(), midPath), ImageUtil.getSmallImage(file.getBytes(), 350));
		String smallPath = String.format(Global.getVisitUrl()+"/%s/%s_s.%s", dir, uuid, fileExt);
		write(Paths.get(Global.getProfile(), smallPath), ImageUtil.getSmallImage(file.getBytes(), 160));
		return midPath;
	}


	public String handleFileUploadNoDomain(MultipartFile file, String dir) throws Exception {
		// 判断文件名
		String fileName = file.getOriginalFilename();
		String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
		String uuid = UUID.randomUUID().toString();
		String path = String.format(Global.getVisitUrl()+"/%s/%s.%s", dir, uuid, fileExt);
		write(Paths.get(Global.getProfile(), path), file.getBytes());
		if(fileName.toLowerCase().endsWith(".jpg")||fileName.toLowerCase().endsWith(".jpeg")
				||fileName.toLowerCase().endsWith(".png")||fileName.toLowerCase().endsWith(".gif")){
			//如果是以这几种图片结尾文件判断是否需要压缩
			String frompath = String.format(Paths.get(Global.getProfile(), path).toString());
			 BufferedImage bufferedImage =ImageIO.read(new File(frompath)); // 通过临时文件获取图片流
			 float  scale =1.00f;
			 if (bufferedImage != null) {
				 Integer width = bufferedImage.getWidth(); // 通过图片流获取图片宽度
				 scale = 1000f/width;
			 }
			//拼接文件路劲
			//String thumbnailFilePathName = thumbnailPathName.substring(0, thumbnailPathName.lastIndexOf(".")) + "_min.jpg";
			try {
				if(scale<1){
					String minpath = String.format(Global.getVisitUrl()+"/%s/%s.%s", dir, uuid+"-min", fileExt);

					String topath = String.format(Paths.get(Global.getProfile(), minpath).toString());
					// Thumbnails.of(thumbnailPathName).size(500,500).toFile(thumbnailFilePathName);//变为400*300,遵循原图比例缩或放到400*某个高度
					//Thumbnails.of(frompath).scale(1f).outputQuality(scale).outputFormat("jpg").toFile(topath);// 图片尺寸不变，压缩图片文件大小outputQuality实现,参数1为最高质量
					Thumbnails.of(frompath).scale(scale).toFile(topath);//按比例缩小
					path = minpath;
				}

			} catch (Exception e1) {

			}
		}
		String ret = Global.getVisitUrl() + path;
		return StringUtils.replaceAll(ret,"//", "/");
	}

	public String handleFileUpload4Ueditor(MultipartFile file) throws Exception {
		// 判断文件名
		String fileName = file.getOriginalFilename();
		String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
		String uuid = UUID.randomUUID().toString();
		String path = String.format(Global.getUeditorUrl()+"/%s.%s", uuid, fileExt);
		write(Paths.get(Global.getProfile(),path), file.getBytes());
		if(fileName.toLowerCase().endsWith(".jpg")||fileName.toLowerCase().endsWith(".jpeg")
				||fileName.toLowerCase().endsWith(".png")||fileName.toLowerCase().endsWith(".gif")){
			//如果是以这几种图片结尾文件判断是否需要压缩
			String frompath = String.format(Paths.get(Global.getProfile(),path).toString());
			BufferedImage bufferedImage =ImageIO.read(new File(frompath)); // 通过临时文件获取图片流
			float  scale =1.00f;
			if (bufferedImage != null) {
				Integer width = bufferedImage.getWidth(); // 通过图片流获取图片宽度
				scale = 1000f/width;
			}
			//拼接文件路劲
			//String thumbnailFilePathName = thumbnailPathName.substring(0, thumbnailPathName.lastIndexOf(".")) + "_min.jpg";
			try {
				if(scale<1){
					String minpath = String.format(Global.getUeditorUrl()+"/%s.%s", uuid+"-min", fileExt);

					String topath = String.format(Paths.get(Global.getProfile(),minpath).toString());
					// Thumbnails.of(thumbnailPathName).size(500,500).toFile(thumbnailFilePathName);//变为400*300,遵循原图比例缩或放到400*某个高度
					//Thumbnails.of(frompath).scale(1f).outputQuality(scale).outputFormat("jpg").toFile(topath);// 图片尺寸不变，压缩图片文件大小outputQuality实现,参数1为最高质量
					Thumbnails.of(frompath).scale(scale).toFile(topath);//按比例缩小
					path = minpath;
				}

			} catch (Exception e1) {
			}
		}
		return Global.getUeditorVisitUrl()+path;
	}


	public void write(Path path, byte[] content) throws IOException {
		if (!Files.exists(path.getParent())) {

			Files.createDirectories(path.getParent());
		}
		Files.write(path, content);
	}

}
